public void SendCommand(IPAddress target, OpcoDes command, string metatype, object metadata) { if (sSocket == null) return; sSocket.Sent += new EventHandler(SignalMutexSent); //mutexSent = new AutoResetEvent(false); try { sSocket.Target = target; sSocket.Command = command; sSocket.Metatype = metatype; sSocket.Metadata = metadata; sSocket.Send(); Trace.WriteLine("[OETS.Client] [SendCommand] --> " + command); mutexSent.WaitOne(); } catch (Exception exc) { Trace.Write(exc.Message); } finally { mutexSent.Set(); } sSocket.Sent -= new EventHandler(SignalMutexSent); }
private void ProcessRecieve(SocketAsyncEventArgs args) { try { var bytesReceived = args.BytesTransferred; if (bytesReceived == 0) { Trace.WriteLine("[OETS.Shared] SSocket ProcessRecieve bytesReceived = 0"); this.Close(); if (Disconnected != null) Disconnected(this, EventArgs.Empty); } else { int offset = 4; //command int bufferVal = BitConverter.ToInt32(_bufferSegment, 0); /*/ IP byte[] b1 = new byte[4]; System.Buffer.BlockCopy(_bufferSegment, offset, b1, 0, 4); offset += b1.Length; int size = BitConverter.ToInt32(b1, 0); byte[] ipBuffer = new byte[size]; System.Buffer.BlockCopy(_bufferSegment, offset, ipBuffer, 0, size); offset += size; */ // metaType byte[] b2 = new byte[4]; System.Buffer.BlockCopy(_bufferSegment, offset, b2, 0, 4); offset += b2.Length; int size = BitConverter.ToInt32(b2, 0); byte[] typeBuffer = new byte[size]; System.Buffer.BlockCopy(_bufferSegment, offset, typeBuffer, 0, size); offset += size; // metadata byte[] b3 = new byte[4]; System.Buffer.BlockCopy(_bufferSegment, offset, b3, 0, 4); offset += b3.Length; size = BitConverter.ToInt32(b3, 0); byte[] metaBuffer = new byte[size]; System.Buffer.BlockCopy(_bufferSegment, offset, metaBuffer, 0, size); offset += size; System.Array.Clear(_bufferSegment, 0, _bufferSegment.Length); // check if it is a know command if (Enum.IsDefined(typeof(OpcoDes), bufferVal)) { command = (OpcoDes)bufferVal; // if (!IPAddress.TryParse(Encoding.Unicode.GetString(ipBuffer), out target)) // target = IPAddress.Parse("127.0.0.1"); // Trace.Write(string.Format("SSocket RECIEVE {0} <- {1} {2}", //socket.LocalEndPoint.ToString(), socket.RemoteEndPoint.ToString(), command.ToString())); metatype = Encoding.Unicode.GetString(typeBuffer); if (String.IsNullOrEmpty(metatype)) { Trace.Write("Ошибка!!!!!! Тип не найден! [" + metatype + "]. Команда - " + command + ". " + " Размер metaBuffer:" + metaBuffer.Length + ", typeBuffer:" + typeBuffer.Length + ",bufferVal:" + bufferVal); } Type typeToCreate = Type.GetType(metatype); IBasePacket packet = (IBasePacket)Activator.CreateInstance(typeToCreate); packet.Initialize(metaBuffer); metadata = packet; if (Received != null) Received(this, EventArgs.Empty); } receivedBytes = (uint)bytesReceived; Receive(); } } catch (Exception ex) { Trace.WriteLine("[OETS.Shared] ProcessRecieve " + ex.Message); Trace.WriteLine("[OETS.Shared] ProcessRecieve " + ex.StackTrace); this.Close(); if (Disconnected != null) Disconnected(this, EventArgs.Empty); } finally { args.Completed -= ReceiveAsyncComplete; SocketHelpers.ReleaseSocketArg(args); } }
/// <summary> /// Deep clone of a ChatSocket object without the network stream cloned. /// It is used when a new ChatSocket object is passed on to methods, events that /// do not need a network stream. /// </summary> public SSocket(SSocket cp) { this.target = cp.Target; this.command = cp.Command; this.metatype = cp.Metatype; this.metadata = cp.Metadata; this.sentBytes = cp.SentBytes; this.receivedBytes = cp.ReceivedBytes; }
public SSocket( ref Socket socket, IPAddress target, OpcoDes command, string metatype, object metadata) : this(ref socket) { this.target = target; this.command = command; this.metatype = metatype; this.metadata = metadata; }
public void SendCommand(ClientManager cm, OpcoDes command, object metadata) { SSocket sSocket = cm.SSocket; if (sSocket == null) return; sSocket.Sent += new EventHandler(SignalMutexSent); //mutexSent = new AutoResetEvent(true); try { sSocket.Command = command; sSocket.Metatype = metadata.GetType().FullName; sSocket.Metadata = metadata; sSocket.Send(); mutexSent.WaitOne(); } catch (Exception exc) { Trace.Write(exc.Message); } finally { mutexSent.Set(); } sSocket.Sent -= new EventHandler(SignalMutexSent); }
/// <summary> /// Using the ClientManager parameter send back a response to the client. /// </summary> private void SendResponse(ClientManager cm, OpcoDes command, string response) { SSocket sSocket = cm.SSocket; ResponsePacket pck = new ResponsePacket(); pck.From = "SSocketServer"; pck.To = cm.UserName; pck.Response = response; SendCommand(cm, command, pck); }