/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/vchannel.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/seamlessrdp/ServerExe/vchannel.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1091 by astrand, Fri Mar 10 09:12:36 2006 UTC revision 1248 by ossman_, Wed Jul 12 12:01:56 2006 UTC
# Line 32  Line 32 
32    
33  #define CHANNELNAME "seamrdp"  #define CHANNELNAME "seamrdp"
34    
35    #define INVALID_CHARS ","
36    #define REPLACEMENT_CHAR '_'
37    
38    #ifdef __GNUC__
39    #define SHARED __attribute__((section ("SHAREDDATA"), shared))
40    #else
41    #define SHARED
42    #endif
43    
44    // Shared DATA
45    #pragma data_seg ( "SHAREDDATA" )
46    
47    unsigned int g_vchannel_serial SHARED = 0;
48    
49    #pragma data_seg ()
50    
51    #pragma comment(linker, "/section:SHAREDDATA,rws")
52    
53  static HANDLE g_mutex = NULL;  static HANDLE g_mutex = NULL;
54  static HANDLE g_vchannel = NULL;  static HANDLE g_vchannel = NULL;
55    static unsigned int g_opencount = 0;
56    
57  void  DLL_EXPORT void
58  debug(char *format, ...)  debug(char *format, ...)
59  {  {
60          va_list argp;          va_list argp;
61          char buf[256];          char buf[256];
62    
         sprintf(buf, "DEBUG,");  
   
63          va_start(argp, format);          va_start(argp, format);
64          _vsnprintf(buf + sizeof("DEBUG,") - 1, sizeof(buf) - sizeof("DEBUG,") + 1, format, argp);          _vsnprintf(buf, sizeof(buf), format, argp);
65          va_end(argp);          va_end(argp);
66    
67          vchannel_write(buf);          vchannel_strfilter(buf);
68    
69            vchannel_write("DEBUG", buf);
70  }  }
71    
72    #define CONVERT_BUFFER_SIZE 1024
73    static char convert_buffer[CONVERT_BUFFER_SIZE];
74    
75    DLL_EXPORT const char *
76    unicode_to_utf8(const unsigned short *string)
77    {
78            unsigned char *buf;
79            size_t size;
80    
81            buf = (unsigned char *) convert_buffer;
82            size = sizeof(convert_buffer) - 1;
83    
84  int          /* We do not handle characters outside BMP (i.e. we can't do UTF-16) */
85            while (*string != 0x0000)
86            {
87                    if (*string < 0x80)
88                    {
89                            if (size < 1)
90                                    break;
91                            *buf++ = (unsigned char) *string;
92                            size--;
93                    }
94                    else if (*string < 0x800)
95                    {
96                            if (size < 2)
97                                    break;
98                            *buf++ = 0xC0 | (*string >> 6);
99                            *buf++ = 0x80 | (*string & 0x3F);
100                            size -= 2;
101                    }
102                    else if ((*string < 0xD800) || (*string > 0xDFFF))
103                    {
104                            if (size < 3)
105                                    break;
106                            *buf++ = 0xE0 | (*string >> 12);
107                            *buf++ = 0x80 | (*string >> 6 & 0x3F);
108                            *buf++ = 0x80 | (*string & 0x3F);
109                            size -= 2;
110                    }
111    
112                    string++;
113            }
114    
115            *buf = '\0';
116    
117            return convert_buffer;
118    }
119    
120    DLL_EXPORT int
121  vchannel_open()  vchannel_open()
122  {  {
123            g_opencount++;
124            if (g_opencount > 1)
125                    return 0;
126    
127          g_vchannel = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE,          g_vchannel = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE,
128                                             WTS_CURRENT_SESSION, CHANNELNAME);                                             WTS_CURRENT_SESSION, CHANNELNAME);
129    
# Line 71  vchannel_open() Line 141  vchannel_open()
141          return 0;          return 0;
142  }  }
143    
144  void  DLL_EXPORT void
145  vchannel_close()  vchannel_close()
146  {  {
147            g_opencount--;
148            if (g_opencount > 0)
149                    return;
150    
151          if (g_mutex)          if (g_mutex)
152                  CloseHandle(g_mutex);                  CloseHandle(g_mutex);
153    
# Line 84  vchannel_close() Line 158  vchannel_close()
158          g_vchannel = NULL;          g_vchannel = NULL;
159  }  }
160    
161  int  DLL_EXPORT int
162  vchannel_is_open()  vchannel_is_open()
163  {  {
164          if (g_vchannel == NULL)          if (g_vchannel == NULL)
# Line 93  vchannel_is_open() Line 167  vchannel_is_open()
167                  return 1;                  return 1;
168  }  }
169    
170  int  DLL_EXPORT int
171  vchannel_read(char *line, size_t length)  vchannel_read(char *line, size_t length)
172  {  {
173          static BOOL overflow_mode = FALSE;          static BOOL overflow_mode = FALSE;
# Line 163  vchannel_read(char *line, size_t length) Line 237  vchannel_read(char *line, size_t length)
237          return 0;          return 0;
238  }  }
239    
240  int  DLL_EXPORT int
241  vchannel_write(char *format, ...)  vchannel_write(const char *command, const char *format, ...)
242  {  {
243          BOOL result;          BOOL result;
244          va_list argp;          va_list argp;
# Line 174  vchannel_write(char *format, ...) Line 248  vchannel_write(char *format, ...)
248    
249          assert(vchannel_is_open());          assert(vchannel_is_open());
250    
251            WaitForSingleObject(g_mutex, INFINITE);
252    
253            size = _snprintf(buf, sizeof(buf), "%s,%u,", command, g_vchannel_serial);
254    
255            assert(size < sizeof(buf));
256    
257          va_start(argp, format);          va_start(argp, format);
258          size = _vsnprintf(buf, sizeof(buf), format, argp);          size += _vsnprintf(buf + size, sizeof(buf) - size, format, argp);
259          va_end(argp);          va_end(argp);
260    
261          assert(size < sizeof(buf));          assert(size < sizeof(buf));
262    
         WaitForSingleObject(g_mutex, INFINITE);  
263          result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written);          result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written);
264          result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written);          result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written);
265    
266            g_vchannel_serial++;
267    
268          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
269    
270          if (!result)          if (!result)
# Line 191  vchannel_write(char *format, ...) Line 273  vchannel_write(char *format, ...)
273          return bytes_written;          return bytes_written;
274  }  }
275    
276  void  DLL_EXPORT void
277  vchannel_block()  vchannel_block()
278  {  {
279          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
280  }  }
281    
282  void  DLL_EXPORT void
283  vchannel_unblock()  vchannel_unblock()
284  {  {
285          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
286  }  }
287    
288    DLL_EXPORT const char *
289    vchannel_strfilter(char *string)
290    {
291            char *c;
292    
293            for (c = string; *c != '\0'; c++)
294            {
295                    if (((unsigned char) *c < 0x20) || (strchr(INVALID_CHARS, *c) != NULL))
296                            *c = REPLACEMENT_CHAR;
297            }
298    
299            return string;
300    }
301    
302    DLL_EXPORT const char *
303    vchannel_strfilter_unicode(const unsigned short *string)
304    {
305            return vchannel_strfilter((char *) unicode_to_utf8(string));
306    }

Legend:
Removed from v.1091  
changed lines
  Added in v.1248

  ViewVC Help
Powered by ViewVC 1.1.26