Skip to content

Commit

Permalink
Final API 967 and beta API 968 Java sources added
Browse files Browse the repository at this point in the history
  • Loading branch information
arvicco committed Oct 29, 2012
1 parent 635ab99 commit 3abff3a
Show file tree
Hide file tree
Showing 169 changed files with 34,463 additions and 0 deletions.
1 change: 1 addition & 0 deletions misc/IBJts-967final/API_VersionNum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_Version=9.67
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "EClientSocketBaseImpl.h"

208 changes: 208 additions & 0 deletions misc/IBJts-967final/cpp/PosixSocketClient/EPosixClientSocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#include "EPosixClientSocket.h"

#include "EPosixClientSocketPlatform.h"
#include "TwsSocketClientErrors.h"
#include "EWrapper.h"

#include <string.h>

///////////////////////////////////////////////////////////
// member funcs
EPosixClientSocket::EPosixClientSocket( EWrapper *ptr) : EClientSocketBase( ptr)
{
m_fd = -1;
}

EPosixClientSocket::~EPosixClientSocket()
{
}

bool EPosixClientSocket::eConnect( const char *host, unsigned int port, int clientId)
{
// reset errno
errno = 0;

// already connected?
if( m_fd >= 0) {
errno = EISCONN;
getWrapper()->error( NO_VALID_ID, ALREADY_CONNECTED.code(), ALREADY_CONNECTED.msg());
return false;
}

// initialize Winsock DLL (only for Windows)
if ( !SocketsInit()) {
return false;
}

// create socket
m_fd = socket(AF_INET, SOCK_STREAM, 0);

// cannot create socket
if( m_fd < 0) {
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg());
return false;
}

// use local machine if no host passed in
if ( !( host && *host)) {
host = "127.0.0.1";
}

// starting to connect to server
struct sockaddr_in sa;
memset( &sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons( port);
sa.sin_addr.s_addr = inet_addr( host);

// try to connect
if( (connect( m_fd, (struct sockaddr *) &sa, sizeof( sa))) < 0) {
// error connecting
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
return false;
}

// set client id
setClientId( clientId);

onConnectBase();

while( isSocketOK() && !isConnected()) {
if ( !checkMessages()) {
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
return false;
}
}

// successfully connected
return true;
}

void EPosixClientSocket::eDisconnect()
{
if ( m_fd >= 0 )
// close socket
SocketClose( m_fd);
m_fd = -1;
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
eDisconnectBase();
}

bool EPosixClientSocket::isSocketOK() const
{
return ( m_fd >= 0);
}

int EPosixClientSocket::fd() const
{
return m_fd;
}

int EPosixClientSocket::send(const char* buf, size_t sz)
{
if( sz <= 0)
return 0;

int nResult = ::send( m_fd, buf, sz, 0);

if( nResult == -1 && !handleSocketError()) {
return -1;
}
if( nResult <= 0) {
return 0;
}
return nResult;
}

int EPosixClientSocket::receive(char* buf, size_t sz)
{
if( sz <= 0)
return 0;

int nResult = ::recv( m_fd, buf, sz, 0);

if( nResult == -1 && !handleSocketError()) {
return -1;
}
if( nResult <= 0) {
return 0;
}
return nResult;
}

///////////////////////////////////////////////////////////
// callbacks from socket

void EPosixClientSocket::onConnect()
{
if( !handleSocketError())
return;

onConnectBase();
}

void EPosixClientSocket::onReceive()
{
if( !handleSocketError())
return;

checkMessages();
}

void EPosixClientSocket::onSend()
{
if( !handleSocketError())
return;

sendBufferedData();
}

void EPosixClientSocket::onClose()
{
if( !handleSocketError())
return;

eDisconnect();
getWrapper()->connectionClosed();
}

void EPosixClientSocket::onError()
{
handleSocketError();
}

///////////////////////////////////////////////////////////
// helper
bool EPosixClientSocket::handleSocketError()
{
// no error
if( errno == 0)
return true;

// Socket is already connected
if( errno == EISCONN) {
return true;
}

if( errno == EWOULDBLOCK)
return false;

if( errno == ECONNREFUSED) {
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
}
else {
getWrapper()->error( NO_VALID_ID, SOCKET_EXCEPTION.code(),
SOCKET_EXCEPTION.msg() + strerror(errno));
}
// reset errno
errno = 0;
eDisconnect();
return false;
}
47 changes: 47 additions & 0 deletions misc/IBJts-967final/cpp/PosixSocketClient/EPosixClientSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef eposixclientsocket_def
#define eposixclientsocket_def

#include "EClientSocketBase.h"

class EWrapper;

class EPosixClientSocket : public EClientSocketBase
{
public:

explicit EPosixClientSocket( EWrapper *ptr);
~EPosixClientSocket();

// override virtual funcs from EClient
bool eConnect( const char *host, unsigned int port, int clientId=0);
void eDisconnect();

bool isSocketOK() const;
int fd() const;

private:

int send( const char* buf, size_t sz);
int receive( char* buf, size_t sz);

public:
// callback from socket
void onReceive();
void onSend();
void onError();

private:

void onConnect();
void onClose();

public:
// helper
bool handleSocketError();

private:

int m_fd;
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef eposixclientsocketcommon_def
#define eposixclientsocketcommon_def

#ifdef _WIN32
// Windows
// includes
#include <WinSock2.h>
#include <time.h>

// defines
#define EISCONN WSAEISCONN
#define EWOULDBLOCK WSAEWOULDBLOCK
#define ECONNREFUSED WSAECONNREFUSED

// helpers
inline bool SocketsInit( void) {
WSADATA data;
return ( !WSAStartup( MAKEWORD(2, 2), &data));
};
inline bool SocketsDestroy() { return ( !WSACleanup()); };
inline int SocketClose(int sockfd) { return closesocket( sockfd); };

#else
// LINUX
// includes
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>

// helpers
inline bool SocketsInit() { return true; };
inline bool SocketsDestroy() { return true; };
inline int SocketClose(int sockfd) { return close( sockfd); };

#endif

#endif
44 changes: 44 additions & 0 deletions misc/IBJts-967final/cpp/PosixSocketClientTest/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifdef _WIN32
# include <Windows.h>
# define sleep( seconds) Sleep( seconds * 1000);
#else
# include <unistd.h>
#endif

#include "PosixTestClient.h"

const unsigned MAX_ATTEMPTS = 50;
const unsigned SLEEP_TIME = 10;

int main(int argc, char** argv)
{
const char* host = argc > 1 ? argv[1] : "";
unsigned int port = 7496;
int clientId = 0;

unsigned attempt = 0;
printf( "Start of POSIX Socket Client Test %u\n", attempt);

for (;;) {
++attempt;
printf( "Attempt %u of %u\n", attempt, MAX_ATTEMPTS);

PosixTestClient client;

client.connect( host, port, clientId);

while( client.isConnected()) {
client.processMessages();
}

if( attempt >= MAX_ATTEMPTS) {
break;
}

printf( "Sleeping %u seconds before next attempt\n", SLEEP_TIME);
sleep( SLEEP_TIME);
}

printf ( "End of POSIX Socket Client Test\n");
}

17 changes: 17 additions & 0 deletions misc/IBJts-967final/cpp/PosixSocketClientTest/Makefile.linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CXX=g++
CXXFLAGS=-DIB_USE_STD_STRING -Wall -Wno-switch
ROOT_DIR=..
BASE_SRC_DIR=${ROOT_DIR}/PosixSocketClient
INCLUDES=-I${ROOT_DIR}/Shared/ -I${BASE_SRC_DIR}
TARGET=PosixSocketClientTest

$(TARGET):
$(CXX) $(CXXFLAGS) $(INCLUDES) -o EClientSocketBase.o -c $(BASE_SRC_DIR)/EClientSocketBase.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o EPosixClientSocket.o -c $(BASE_SRC_DIR)/EPosixClientSocket.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o PosixTestClient.o -c PosixTestClient.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o Main.o -c Main.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ EClientSocketBase.o EPosixClientSocket.o PosixTestClient.o Main.o

clean:
rm -f $(TARGET) *.o

19 changes: 19 additions & 0 deletions misc/IBJts-967final/cpp/PosixSocketClientTest/Makefile.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CXX=cl
LINK=link
CXXFLAGS=/D IB_USE_STD_STRING /D _CRT_SECURE_NO_DEPRECATE /EHsc /wd4355 /wd4800
ROOT_DIR=..
BASE_SRC_DIR=$(ROOT_DIR)/PosixSocketClient
INCLUDES=/I $(ROOT_DIR)/Shared/ /I $(ROOT_DIR)/PosixSocketClient/
OUTPUT=PosixSocketClientTest.exe

all:
$(CXX) /c $(BASE_SRC_DIR)/EClientSocketBase.cpp $(INCLUDES) $(CXXFLAGS)
$(CXX) /c $(BASE_SRC_DIR)/EPosixClientSocket.cpp $(INCLUDES) $(CXXFLAGS)
$(CXX) /c PosixTestClient.cpp $(INCLUDES) $(CXXFLAGS)
$(CXX) /c Main.cpp $(INCLUDES) $(CXXFLAGS)
$(LINK) EClientSocketBase.obj EPosixClientSocket.obj PosixTestClient.obj Main.obj /OUT:$(OUTPUT)

clean:
del EClientSocketBase.obj EPosixClientSocket.obj PosixTestClient.obj Main.obj $(OUTPUT)


Loading

0 comments on commit 3abff3a

Please sign in to comment.