mirror of
https://github.com/jeremysrand/Listener.git
synced 2024-08-31 17:29:00 +00:00
It more or less works now.
This commit is contained in:
parent
8be6497191
commit
c4274bdbe2
130
Listener/main.c
130
Listener/main.c
@ -47,7 +47,9 @@ typedef enum tListenState {
|
|||||||
LISTEN_STATE_NETWORK_UNCONNECTED,
|
LISTEN_STATE_NETWORK_UNCONNECTED,
|
||||||
LISTEN_STATE_NETWORK_CONNECTED,
|
LISTEN_STATE_NETWORK_CONNECTED,
|
||||||
LISTEN_STATE_AWAITING_CONNECTION,
|
LISTEN_STATE_AWAITING_CONNECTION,
|
||||||
|
LISTEN_STATE_AWAITING_ESTABLISH,
|
||||||
LISTEN_STATE_AWAITING_MSG_HEADER,
|
LISTEN_STATE_AWAITING_MSG_HEADER,
|
||||||
|
LISTEN_STATE_AWAITING_TEXT,
|
||||||
|
|
||||||
LISTEN_STATE_ERROR,
|
LISTEN_STATE_ERROR,
|
||||||
|
|
||||||
@ -102,7 +104,9 @@ void handleStartState(void);
|
|||||||
void handleNetworkUnconnectedState(void);
|
void handleNetworkUnconnectedState(void);
|
||||||
void handleNetworkConnectedState(void);
|
void handleNetworkConnectedState(void);
|
||||||
void handleAwaitingConnectionState(void);
|
void handleAwaitingConnectionState(void);
|
||||||
|
void handleAwaitingEstablishState(void);
|
||||||
void handleAwaitingMsgHeaderState(void);
|
void handleAwaitingMsgHeaderState(void);
|
||||||
|
void handleAwaitingTextState(void);
|
||||||
void handleErrorState(void);
|
void handleErrorState(void);
|
||||||
|
|
||||||
|
|
||||||
@ -115,7 +119,9 @@ static tStateHandler stateHandlers[NUM_LISTEN_STATES] = {
|
|||||||
handleNetworkUnconnectedState,
|
handleNetworkUnconnectedState,
|
||||||
handleNetworkConnectedState,
|
handleNetworkConnectedState,
|
||||||
handleAwaitingConnectionState,
|
handleAwaitingConnectionState,
|
||||||
|
handleAwaitingEstablishState,
|
||||||
handleAwaitingMsgHeaderState,
|
handleAwaitingMsgHeaderState,
|
||||||
|
handleAwaitingTextState,
|
||||||
|
|
||||||
handleErrorState,
|
handleErrorState,
|
||||||
};
|
};
|
||||||
@ -124,7 +130,9 @@ static char * stateMessages[NUM_LISTEN_STATES] = {
|
|||||||
"Connecting to network",
|
"Connecting to network",
|
||||||
"Creating listen socket",
|
"Creating listen socket",
|
||||||
"Waiting for connection",
|
"Waiting for connection",
|
||||||
|
"Establishing connection",
|
||||||
"Connected to device",
|
"Connected to device",
|
||||||
|
"Receiving text",
|
||||||
|
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
@ -333,6 +341,7 @@ void handleStartState(void)
|
|||||||
|
|
||||||
void handleNetworkUnconnectedState(void)
|
void handleNetworkUnconnectedState(void)
|
||||||
{
|
{
|
||||||
|
TCPIPPoll();
|
||||||
TCPIPConnect(NULL);
|
TCPIPConnect(NULL);
|
||||||
if ((!toolerror()) &&
|
if ((!toolerror()) &&
|
||||||
(TCPIPGetConnectStatus())) {
|
(TCPIPGetConnectStatus())) {
|
||||||
@ -348,12 +357,19 @@ void handleNetworkConnectedState(void)
|
|||||||
{
|
{
|
||||||
Word error;
|
Word error;
|
||||||
|
|
||||||
globals->listenIpid = TCPIPLogin(userId, 0, LISTEN_PORT, 0, 64);
|
TCPIPPoll();
|
||||||
|
globals->listenIpid = TCPIPLogin(userId, 0, 0, 0, 64);
|
||||||
if (toolerror()) {
|
if (toolerror()) {
|
||||||
enterErrorState("Unable to create socket", toolerror());
|
enterErrorState("Unable to create socket", toolerror());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TCPIPSetSourcePort(globals->listenIpid, LISTEN_PORT);
|
||||||
|
if (toolerror()) {
|
||||||
|
enterErrorState("Unable to set port number", toolerror());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
error = TCPIPListenTCP(globals->listenIpid);
|
error = TCPIPListenTCP(globals->listenIpid);
|
||||||
if (error != terrOK) {
|
if (error != terrOK) {
|
||||||
TCPIPLogout(globals->listenIpid);
|
TCPIPLogout(globals->listenIpid);
|
||||||
@ -367,12 +383,12 @@ void handleNetworkConnectedState(void)
|
|||||||
|
|
||||||
void handleAwaitingConnectionState(void)
|
void handleAwaitingConnectionState(void)
|
||||||
{
|
{
|
||||||
|
TCPIPPoll();
|
||||||
globals->connIpid = TCPIPAcceptTCP(globals->listenIpid, 0);
|
globals->connIpid = TCPIPAcceptTCP(globals->listenIpid, 0);
|
||||||
switch (toolerror()) {
|
switch (toolerror()) {
|
||||||
case terrOK:
|
case terrOK:
|
||||||
globals->hasConnIpid = TRUE;
|
globals->hasConnIpid = TRUE;
|
||||||
newState(LISTEN_STATE_AWAITING_MSG_HEADER);
|
newState(LISTEN_STATE_AWAITING_ESTABLISH);
|
||||||
globals->position = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case terrNOINCOMING:
|
case terrNOINCOMING:
|
||||||
@ -385,9 +401,54 @@ void handleAwaitingConnectionState(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void handleAwaitingEstablishState(void)
|
||||||
|
{
|
||||||
|
static srBuff srBuffer;
|
||||||
|
|
||||||
|
TCPIPPoll();
|
||||||
|
|
||||||
|
TCPIPStatusTCP(globals->connIpid, &srBuffer);
|
||||||
|
if (toolerror()) {
|
||||||
|
enterErrorState("Unable to get connection state", toolerror());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (srBuffer.srState) {
|
||||||
|
case TCPSSYNSENT:
|
||||||
|
case TCPSSYNRCVD:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TCPSESTABLISHED:
|
||||||
|
newState(LISTEN_STATE_AWAITING_MSG_HEADER);
|
||||||
|
globals->position = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
enterErrorState("Unexpected TCP state", srBuffer.srState);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void handleAwaitingMsgHeaderState(void)
|
void handleAwaitingMsgHeaderState(void)
|
||||||
{
|
{
|
||||||
rrBuff readResponseBuf;
|
static srBuff srBuffer;
|
||||||
|
static rrBuff readResponseBuf;
|
||||||
|
tTextList * textTransfer;
|
||||||
|
|
||||||
|
TCPIPPoll();
|
||||||
|
|
||||||
|
TCPIPStatusTCP(globals->connIpid, &srBuffer);
|
||||||
|
if (toolerror()) {
|
||||||
|
enterErrorState("Unable to get connection state", toolerror());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (srBuffer.srState != TCPSESTABLISHED) {
|
||||||
|
TCPIPAbortTCP(globals->connIpid);
|
||||||
|
TCPIPLogout(globals->connIpid);
|
||||||
|
globals->hasConnIpid = FALSE;
|
||||||
|
newState(LISTEN_STATE_AWAITING_CONNECTION);
|
||||||
|
return;
|
||||||
|
}
|
||||||
Word error = TCPIPReadTCP(globals->connIpid, 0,
|
Word error = TCPIPReadTCP(globals->connIpid, 0,
|
||||||
((uint32_t)(&(globals->messageHeader))) + globals->position,
|
((uint32_t)(&(globals->messageHeader))) + globals->position,
|
||||||
sizeof(globals->messageHeader) - globals->position,
|
sizeof(globals->messageHeader) - globals->position,
|
||||||
@ -408,10 +469,20 @@ void handleAwaitingMsgHeaderState(void)
|
|||||||
else
|
else
|
||||||
globals->line2[0] = '\0';
|
globals->line2[0] = '\0';
|
||||||
InvalidateWindow();
|
InvalidateWindow();
|
||||||
|
globals->position = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LISTEN_TEXT_MSG:
|
case LISTEN_TEXT_MSG:
|
||||||
// TODO - Transition to a new state when getting text
|
textTransfer = malloc(sizeof(tTextListHeader) + globals->messageHeader.messageArg);
|
||||||
|
if (textTransfer == NULL) {
|
||||||
|
enterErrorState("Unable to get memory for text", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
globals->textTransfer = textTransfer;
|
||||||
|
textTransfer->header.next = NULL;
|
||||||
|
textTransfer->header.size = globals->messageHeader.messageArg;
|
||||||
|
textTransfer->header.position = 0;
|
||||||
|
newState(LISTEN_STATE_AWAITING_TEXT);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -419,6 +490,54 @@ void handleAwaitingMsgHeaderState(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleAwaitingTextState(void)
|
||||||
|
{
|
||||||
|
static srBuff srBuffer;
|
||||||
|
static rrBuff readResponseBuf;
|
||||||
|
tTextList * textTransfer = globals->textTransfer;
|
||||||
|
|
||||||
|
TCPIPPoll();
|
||||||
|
|
||||||
|
TCPIPStatusTCP(globals->connIpid, &srBuffer);
|
||||||
|
if (toolerror()) {
|
||||||
|
enterErrorState("Unable to get connection state", toolerror());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (srBuffer.srState != TCPSESTABLISHED) {
|
||||||
|
TCPIPAbortTCP(globals->connIpid);
|
||||||
|
TCPIPLogout(globals->connIpid);
|
||||||
|
globals->hasConnIpid = FALSE;
|
||||||
|
newState(LISTEN_STATE_AWAITING_CONNECTION);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Word error = TCPIPReadTCP(globals->connIpid, 0,
|
||||||
|
(uint32_t)(&(textTransfer->text[textTransfer->header.position])),
|
||||||
|
textTransfer->header.size - textTransfer->header.position,
|
||||||
|
&readResponseBuf);
|
||||||
|
if (error != tcperrOK) {
|
||||||
|
enterErrorState("Unable to read text from connection", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
textTransfer->header.position += readResponseBuf.rrBuffCount;
|
||||||
|
if (textTransfer->header.position < textTransfer->header.size)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (globals->textListTail != NULL) {
|
||||||
|
globals->textListTail->header.next = textTransfer;
|
||||||
|
} else {
|
||||||
|
globals->textListHead = textTransfer;
|
||||||
|
strcpy(globals->line3, " Typing...");
|
||||||
|
InvalidateWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
textTransfer->header.position = 0;
|
||||||
|
globals->textListTail = textTransfer;
|
||||||
|
globals->textTransfer = NULL;
|
||||||
|
newState(LISTEN_STATE_AWAITING_MSG_HEADER);
|
||||||
|
globals->position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void handleErrorState(void)
|
void handleErrorState(void)
|
||||||
{
|
{
|
||||||
@ -428,7 +547,6 @@ void handleErrorState(void)
|
|||||||
|
|
||||||
void runStateMachine(void)
|
void runStateMachine(void)
|
||||||
{
|
{
|
||||||
TCPIPPoll();
|
|
||||||
stateHandlers[globals->state]();
|
stateHandlers[globals->state]();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user