Tuesday, July 5, 2011

Disable Nagle algorithm (TCP_NODELAY) in Cocoa AsyncSocket library

#import // for IPPROTO_TCP
#include // for TCP_NODELAY
- (void)onSocket:(AsyncSocket *)sock
didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"client - didConnectToHost");
// etc etc ..

// Add exactly the following code to disable Nagling:
CFSocketRef cfsock = [sock getCFSocket];
CFSocketNativeHandle rawsock = CFSocketGetNative(cfsock);
int flag = 1;
int result = setsockopt(rawsock, IPPROTO_TCP, TCP_NODELAY,
(char *)&flag, sizeof(int));
if (result != 0)
NSLog(@"Could Not Disable Nagle...");
else
NSLog(@"Nagle Is Disabled.");
}
}
Note that you need the two includes, just for the two constants mentioned in the code. Source

Wednesday, October 20, 2010

[Bada] Variable height row in custom list

Use EnrichedText class to display multi-line text in a row, and call GetTotalLineHeight() to get the height of the row. (view more)

EnrichedText titleText;
titleText.Construct(Dimension(200, 150));
titleText.SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
titleText.SetTextWrapStyle(TEXT_WRAP_WORD_WRAP); // Wrap the text
titleText.SetTextAbbreviationEnabled(false); // no abbreviation

TextElement titleTextElement;
titleTextElement.Construct(text1);
Font f;
f.Construct(FONT_STYLE_PLAIN, 24);
titleTextElement.SetFont(f);
titleText.Add(titleTextElement);

AppLog("HEIGHT %i", titleText.GetTotalLineHeight());

Thursday, August 26, 2010

Create a simple dialog in Tkinter (python)

To create a dialog in Python using Tkinter module, just inherit from simpledialog.Dialog class from tkinter.simpledialog module (Python 3).

from tkinter import *
from tkinter import simpledialog
import sys, os

class SettingDialog(simpledialog.Dialog):
def body(self, master):
Label(master, text="First").grid(row=0, sticky=W)

root = Tk()
#Without "default" keyword, only root (parent) window icon is changed
#other child dialogs still use Tk icon
root.iconbitmap(default='AC.ico')
#call withdraw() to remove parent window, only show the child dialog.
root.withdraw()
SettingDialog(root, "hello")

Wednesday, August 25, 2010

cx_Freeze and Python executable

To make the executable created by cx_Freeze on Python code works on other computers, you have to copy the following files to the executable directory:
  • C:\WINDOWS\WinSxS\Manifests\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022. 8_x-ww_d08d0375.manifest
  • C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022 .8_x-ww_d08d0375\
    msvcm90.dll
    msvcp90.dll
    msvcr90.dll

Friday, August 20, 2010

Create Single Instance Python Application in Windows

Source by Dragan Jovelic.

from win32event import CreateMutex
from win32api import CloseHandle, GetLastError
from winerror import ERROR_ALREADY_EXISTS

class singleinstance:
""" Limits application to single instance """

def __init__(self):
self.mutexname = "testmutex_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"
self.mutex = CreateMutex(None, False, self.mutexname)
self.lasterror = GetLastError()
def aleradyrunning(self):
return (self.lasterror == ERROR_ALREADY_EXISTS)
def __del__(self):
if self.mutex:
CloseHandle(self.mutex)


#---------------------------------------------#
# sample usage:
#

from singleinstance import singleinstance
from sys import exit

# do this at beginnig of your application
myapp = singleinstance()

# check is another instance of same program running
if myapp.aleradyrunning():
print "Another instance of this program is already running"
exit(0)

# not running, safe to continue...
print "No another instance is running, can continue here"