Ejemplo n.º 1
0
        private static void test()
        {
            var con = NetServerFactory.IConnectionFactory("127.0.0.1", 4321, (key) =>
            {
                key.OnOpen    = () => { Console.WriteLine("sdf"); };
                key.OnMessage = dt =>
                {
                    Console.WriteLine(Encoding.UTF8.GetString(dt.datalist));
                };
                key.OnClose = () =>
                {
                    Console.WriteLine("close");
                };
            });

            while (true)
            {
                var line = Console.ReadLine();
                var dsdf = new dataStruct()
                {
                    datalist   = Encoding.UTF8.GetBytes(line),
                    StreamType = StreamTypes.message
                };
                if (line.Equals("close"))
                {
                    con.Close();
                    continue;
                }
                con.Send(dsdf);
            }
        }
Ejemplo n.º 2
0
 private static void C_reciveevent(dataStruct ds)
 {
     if (ds.StreamType == StreamTypes.message)
     {
         Console.WriteLine(Encoding.Default.GetString(ds.datalist));
     }
 }
Ejemplo n.º 3
0
 public Task Send(dataStruct message)
 {
     return(this.Socket.Send(message, () =>
     {
     }, e =>
     {
         Close();
     }));
 }
Ejemplo n.º 4
0
        public dataStruct Recive(Action <dataStruct> action)
        {
            dataStruct ds = new dataStruct();

            var res = ds.GetDatastruct(NetWorkStream);

            //reciveevent(res);
            action(res);
            return(res);
        }
Ejemplo n.º 5
0
 public static byte[] DataStructToByte(dataStruct res)
 {
     byte[] head = new byte[headLength + res.Data_Length];
     head[dataStruct.filetypeindex] = (byte)res.StreamType;
     byte[] length = BitConverter.GetBytes(res.Data_Length);
     length.CopyTo(head, dataStruct.filelengthindex);
     if (res.FileName != null)
     {
         var fl = Encoding.UTF8.GetBytes(res.FileName);
         fl.CopyTo(head, dataStruct.filenameindex);
     }
     res.datalist.CopyTo(head, headLength);
     return(head);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 返回datastruct
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static dataStruct byteToDataStruct(byte[] b)
        {
            dataStruct dt = new dataStruct();

            dt.StreamType = (StreamTypes)b[dataStruct.filetypeindex];
            int datalength = BitConverter.ToInt32(b, dataStruct.filelengthindex);

            dt.datalist = new byte[datalength];
            int fc = dataStruct.headLength - dataStruct.filenameindex;

            if (dt.StreamType == StreamTypes.bin)
            {
                byte[] filename = new byte[fc];
                for (int i = 0; i < dataStruct.headLength - dataStruct.filenameindex; i++)
                {
                    filename[i] = b[i + dataStruct.filenameindex];
                }
                dt.FileName = Encoding.Default.GetString(filename);
            }
            return(dt);
        }
Ejemplo n.º 7
0
 public async Task SendAsync(dataStruct ds)
 {
     byte[] b = ds.GetStream();
     await NetWorkStream.WriteAsync(b, 0, b.Length);
 }
Ejemplo n.º 8
0
        public void Send(dataStruct ds)
        {
            byte[] b = ds.GetStream();

            NetWorkStream.Write(b, 0, b.Length);
        }