static private bool receive(Socket client, CMessageHandler m) { // Create the state object. StateObject state = new StateObject(m, client); bool ok = false; try { state.bytesRead = 0; if (bufferRx == null) { bufferRx = new byte[BufferSize]; } // Begin receiving the data from the remote device. client.BeginReceive(bufferRx, 0, BufferSize, 0, new AsyncCallback(receiveCallback), state); m.ReceiveDone.WaitOne(); ok = !state.Error; } catch (Exception e) { debug(m.Name + " " + e.ToString()); } //state.Dispose(); return(ok); }
static public bool SendToBuddy(string myName, bool forceReconnect, string buddyIp, string buddyFullName, string msg, Socket client) { #if !DEBUG_LOOPBACK if (buddyIp == myIP) { return(true); // just ignore this situation } #endif string fullMsg = CMessageHandler.GenerateMessage(myName, myIP, msg); if (client != null || forceReconnect) { #if DEBUG_LOOPBACK if (ipBuddyMessages.Count > 0 && ipsInUse.Contains(buddyIp)) { return(false); } #else if (ipsInUse.Contains(buddyIp)) { return(false); } #endif // this is a new connection! //if (!ipBuddyMessages.ContainsKey(buddyIp)) // ipBuddyMessages.Add(buddyIp, new Queue<string>()); //ipBuddyMessages[buddyIp].Enqueue(fullMsg); if (!String.IsNullOrEmpty(buddyFullName) && !ipBuddyFullNameLookup.ContainsKey(buddyIp)) { ipBuddyFullNameLookup[buddyIp] = buddyFullName; } debug("Client starting for " + buddyIp); Task.Factory.StartNew(() => StartClientSide(client, buddyIp)); } else { if (ipsInUse.Contains(buddyIp)) { lock (ipBuddyMessages[buddyIp]) { ipBuddyMessages[buddyIp].Enqueue(fullMsg); } } else { //#if !DEBUG_LOOPBACK //Task.Factory.StartNew(() => StartClientSide(buddyIp)); //#endif return(false); // signal that this message did not go thru } } return(true); }
static private void send(Socket client, String data, CMessageHandler m) { // Create the state object. StateObject state = new StateObject(m, client); // Convert the string data to byte data using ASCII encoding. byte[] byteData = Encoding.ASCII.GetBytes(data); // Begin sending the data to the remote device. client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(sendCallback), state); m.SendDone.WaitOne(); m.SendDone.Reset(); state.Dispose(); }
static private void messageHandling(CMessageHandler m, MessageEventArgs mea) { if (mea == null) { } else if (mea.Valid && !String.IsNullOrWhiteSpace(mea.TextFromFriend)) { ipBuddyFullNameLookup[mea.FriendIP] = mea.FriendName; OnNewDataReceived(mea); OnAckReceived(mea); // Update good ip list as needed CSavedIPs.AppendIP(mea.FriendIP); // Signal that all bytes have been received. m.ReceiveDone.Set(); } else { OnAckReceived(mea); } }
public StateObject(CMessageHandler mh, Socket socket) { m = mh; workSocket = socket; }
static public void StartClientSide(Socket sender, string buddyIp) { beginThread(); debug("Client thread launched"); int errors = 0; try { #if !DEBUG_LOOPBACK if (!checkForRunningThread(buddyIp)) { endThread(); debug("Client ignored this ip: " + buddyIp); return; } #endif if (!ipBuddyMessages.ContainsKey(buddyIp)) { ipBuddyMessages.Add(buddyIp, new Queue <string>()); } if (!ipBuddyIsTyping.ContainsKey(buddyIp)) { ipBuddyIsTyping.Add(buddyIp, 0); } ipBuddyMessages[buddyIp].Enqueue(CMessageHandler.GenerateMessage(myPCName, myIP, "Connected!")); CMessageHandler m = new CMessageHandler("ClientMH", buddyIp, myIP); // Enter the working loop while (stayAlive) { if (ipBuddyMessages[buddyIp].Count > 0) { lock (ipBuddyMessages[buddyIp]) { try { m.AddMessageToSend(ipBuddyMessages[buddyIp].Dequeue()); } catch { } } } MessageEventArgs mea; CMessageHandler.MsgState state = m.ProcessStates(out mea); messageHandling(m, mea); // Write to Server as needed if (state == CMessageHandler.MsgState.ReadyForRemote || state == CMessageHandler.MsgState.SendAck) { debug("Client sent out msg to " + buddyIp); send(sender, m.MessageReady, m); } else { if (myTypingStatus > 0) { send(sender, "\t", m); } else { send(sender, " ", m); } Thread.Sleep(150); // slow down pinging } if (!receive(sender, m)) { if (errors++ >= 3) { debug("Client error " + buddyIp); break; } } else { errors = 0; } CSavedIPs.AppendIP(buddyIp); } sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (Exception e) { debug("Client exception for " + buddyIp + e.ToString()); if (_debug != null && ipBuddyFullNameLookup.ContainsKey(buddyIp)) { _debug.InjectTestMessage("User aborted: " + ipBuddyFullNameLookup[buddyIp]); } } lock (ipsInUse) { if (ipsInUse.Contains(buddyIp)) { ipsInUse.Remove(buddyIp); } } //lock (ipBuddyMessages) //{ // if (ipBuddyMessages.ContainsKey(buddyIp)) // ipBuddyMessages.Remove(buddyIp); //} endThread(); }
static private void StartServerSide(Socket handler, string buddyIp) { beginThread(); Thread.Sleep(50); debug("Server thread launched"); int errors = 0; try { CMessageHandler m = new CMessageHandler("ServerMH", buddyIp, myIP); if (!ipBuddyMessages.ContainsKey(buddyIp)) { ipBuddyMessages.Add(buddyIp, new Queue <string>()); } if (!ipBuddyIsTyping.ContainsKey(buddyIp)) { ipBuddyIsTyping.Add(buddyIp, 0); } ipBuddyMessages[buddyIp].Enqueue(CMessageHandler.GenerateMessage(myPCName, myIP, "Connected!")); handler.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false); // An incoming connection needs to be processed. while (stayAlive) { if (ipBuddyMessages[buddyIp].Count > 0) { lock (ipBuddyMessages[buddyIp]) { try { m.AddMessageToSend(ipBuddyMessages[buddyIp].Dequeue()); } catch { } } } MessageEventArgs mea; CMessageHandler.MsgState state = m.ProcessStates(out mea); messageHandling(m, mea); // Write to Client as needed if (state == CMessageHandler.MsgState.ReadyForRemote || state == CMessageHandler.MsgState.SendAck) { debug("Server sent out msg to " + buddyIp); send(handler, m.MessageReady, m); } else { if (myTypingStatus > 0) { send(handler, "\t", m); } else { send(handler, " ", m); } Thread.Sleep(150); // slow down pinging } if (!receive(handler, m)) { if (errors++ >= 3) { debug("Server error " + buddyIp); break; } } else { errors = 0; } CSavedIPs.AppendIP(buddyIp); } } catch (Exception e) { debug("Server exception for " + buddyIp + e.ToString()); if (_debug != null && ipBuddyFullNameLookup.ContainsKey(buddyIp)) { _debug.InjectTestMessage("User aborted: " + ipBuddyFullNameLookup[buddyIp]); } } if (handler != null) { handler.Shutdown(SocketShutdown.Both); handler.Disconnect(false); handler.Close(); //handler.Dispose(); } lock (ipsInUse) { if (ipsInUse.Contains(buddyIp)) { ipsInUse.Remove(buddyIp); } } //lock (ipBuddyMessages) //{ // if (ipBuddyMessages.ContainsKey(buddyIp)) // ipBuddyMessages.Remove(buddyIp); //} endThread(); }
public void Dispose() { m = null; workSocket = null; disposed = true; }